package com.example.day7;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
TabLayout tb;

    private  ArrayList<Fragment> fragment_list = new ArrayList<>();
    public ArrayList<String> title_list=new ArrayList<>();//标题
    ViewPager vp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tb=findViewById(R.id.table_layout);
        vp=findViewById(R.id.vp);

        title_list.add("苏为");
        title_list.add("天宇");
        title_list.add("金河");

        Fragment1 f1 = new Fragment1("天宇");
        Fragment1 f2 = new Fragment1("成迁移");
        Fragment1 f3 = new Fragment1("王泽云");

        fragment_list.add(f1);
        fragment_list.add(f2);
        fragment_list.add(f3);
        MyAdapter mad=new MyAdapter(getSupportFragmentManager(),fragment_list,title_list);
        vp.setAdapter(mad);

        tb.setupWithViewPager(vp);
        tb.getTabAt(0).setIcon(R.drawable.selector_actions);
        tb.getTabAt(1).setIcon(R.drawable.selector_actions2);
        tb.getTabAt(2).setIcon(R.drawable.selector_actions3);

        title_list.add("ddddd");
        Fragment1 f4 = new Fragment1("aaaaa");
        fragment_list.add(f4);
        mad.notifyDataSetChanged();

        f1.changeData("rrrr");
        f2.changeData("ttttt");
        mad.notifyDataSetChanged();




        tb.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                添加选中Tab的逻辑
                Toast.makeText(MainActivity.this,"####"+title_list.get(tab.getPosition()),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
//                添加未选中Tab的逻辑
                Toast.makeText(MainActivity.this,"####"+title_list.get(tab.getPosition()),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
//                再次选中tab的逻辑
            }
        });
    }


}
